package com.nowcoder.community.utils;

import com.nowcoder.community.entity.User;
import org.springframework.stereotype.Component;

/*
* 在线程中保存用户对象，使同一个请求的多个方法可以获取用户对象，并保证线程隔离
* */
@Component
public class HostHolder {
    private ThreadLocal<User> threadLocal = new ThreadLocal<>();

    public void setUser(User user){
        threadLocal.set(user);
    }

    public User getUser(){
        return threadLocal.get();
    }

    public void clean(){
        threadLocal.remove();
    }
}
